package com.oyesgo.statistics.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * @FileName DateUtils.java
 * @Description: 格式化时间类
 * @author rose
 * @version 1.0
 * <p>Company: OYESGO</p>
 * <p>Copyright: Copyright (c) 2010</p>
 */
public class DateUtils {
	public static final String DATE_FORMAT_8 = "yyyyMMdd";
	public static final String DATE_FORMAT_10 = "yyyy-MM-dd";
	public static final String DATE_FORMAT_11 = "yyyy年MM月dd日";
	public static final String DATE_FORMAT_17 = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_21 = "yyyy年MM月dd日 HH时mm分ss秒";
	
	/**
	 * 输出的显示格式为2008年08月08日
	 * @param date
	 * @return
	 */
	public static String simpleDateFormat11(Date date){
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_11);
		String str = format.format(date);
		return str;
	}
	/**
	 * 输出的显示格式为2008-08-08
	 * @param date
	 * @return
	 */
	public static String simpleDateFormat10(Date date){
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_10);
		String str = format.format(date);
		return str;
	}
	/**
	 * 输出的显示格式为20080808
	 * @param date
	 * @return
	 */
	public static String simpleDateFormat8(Date date){
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_8);
		String str = format.format(date);
		return str;
	}
	

	
	/**
	 * 输出的显示格式为2008-08-08 20:08:08
	 * @param date
	 * @return
	 */
	public static String simpleDateFormat17(Date date){
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_17);
		String str = format.format(date);
		return str;
	}
	
	/**
	 * 输出的显示格式为2008年08月08日 20时08分08秒
	 * @param date
	 * @return
	 */
	public static String simpleDateFormat21(Date date){
		if (date == null) {
			date = new Date();
		}
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_21);
		String str = format.format(date);
		return str;
	}
	/**
	 * 将2008-08-08格式的字符串转换成日期类型并返回
	 * @param dateString
	 * @return
	 */
	public static Date getDateByDateFormat10(String dateString){
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_10);
		Date date = null;
		try {
			date = format.parse(dateString);
		} catch (ParseException e) {
			System.out.println("exeception:日期转换异常");
		}
		return date;
	}
	
	/**
	 * 将2008-08-08 20:08:08格式的字符串转换成日期类型并返回
	 * @param dateString
	 * @return
	 */
	public static Date getDateByDateFormat17(String dateString){
		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_17);
		Date date = null;
		try {
			date = format.parse(dateString);
		} catch (ParseException e) {
			System.out.println("exeception:日期转换异常");
		}
		return date;
	}
	
	/**
	 * 
	 * @param dateString 原始时间字符串
	 * @param formatString1 原始时间格式
	 * @param formatString2 要得到的时间格式
	 * @return
	 */
	public final static String changeFormat(String dateString,String formatString1,String formatString2){
		String result = "";
		SimpleDateFormat format1 = new SimpleDateFormat(formatString1);
		SimpleDateFormat format2 = new SimpleDateFormat(formatString2);
		try {
			Date date = format1.parse(dateString);
			result = format2.format(date);
		} catch (ParseException e) {
			System.out.println("时间格式错误");
		}
		return result;
	}
	/**
	 * 按照格式得到昨天日期
	 * @param type
	 * @return
	 * String
	 * @author rose
	 */
	public static String getYesterdayByType(String type){
		String yesterdayStr = "";
		if (type == null || "".equals(type)) {
			return yesterdayStr;
		}
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleFormat = new SimpleDateFormat(type);
		Date today = new Date();
		calendar.setTime(today);
		calendar.add(calendar.DATE, -1);//得到昨天日期
		yesterdayStr = simpleFormat.format(calendar.getTime());
		return yesterdayStr;
	}
	/**
	 * 按照格式得到今天向前或者向后的指定日期
	 * @param type
	 * @return
	 * String
	 * @author rose
	 */
	public static String getYesterdayByType(String type,int n){
		String yesterdayStr = "";
		if (type == null || "".equals(type)) {
			return yesterdayStr;
		}
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleFormat = new SimpleDateFormat(type);
		Date today = new Date();
		calendar.setTime(today);
		calendar.add(calendar.DATE, n);//得到今天以前或者以后n天的日期
		yesterdayStr = simpleFormat.format(calendar.getTime());
		return yesterdayStr;
	}
	
	public static void main(String[] args) {
		String yesterdayStr = getYesterdayByType("yyyy-MM-dd");
		System.out.println(yesterdayStr);
	}

}
